﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsAppOpenFile
{
    static class Program
    {
        static Config config = new Config(); 
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            config.InitDomainConfig();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);            
            Form1 form = new Form1();
            form.initData(args[0], "", "");
            string url = args[0];
            if (url.IndexOf("file=") != -1)
            {
                InitOpenFile(args);
            }
            else
            {
                //AutoPrinter autoPrint = new AutoPrinter();
                //autoPrint.PrintText("这是要自动打印的内容",  GValues.printName);
                //Application.Run(form);
                PrinterUsage.PrintTable(args[0]);
                
            }
        }
        static void InitOpenFile(string[] args)
        {
            if (args.Length > 0)
            {
                for (int i = 0; i < args.Length; i++)
                {
                    //MessageBox.Show("传入参数: " + args[i]);
                }

                string url = args[0];
                Console.WriteLine("Received URL: " + url);

                // 解析参数
                string filePath = ParseFilePath(url);

                if (!string.IsNullOrEmpty(filePath))
                {
                    // 打开文件
                    OpenFile(filePath);
                }
            }
            else
            {
                MessageBox.Show("No arguments provided.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        static string ParseFilePath(string url)
        {
            // 假设 URL 格式为 myapp://open?file=C:\Path\To\Your\File.txt
            int startIndex = url.IndexOf("file=") + 5;
            if (startIndex >= 5)
            {
                return Uri.UnescapeDataString( url.Substring(startIndex));
            }
            return null;
        }

        static void OpenFile(string filePath)
        {
            MessageBox.Show(filePath);
            string cadExePath = GValues.AcadPath; // @"E:\Program Files\Autodesk\AutoCAD 2014\acad.exe";
            try
            {
                Process.Start(new ProcessStartInfo
                {
                    FileName = filePath,
                    Arguments =   cadExePath,
                    UseShellExecute = true
                });
                //MessageBox.Show("File opened successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error opening file: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
